package com.cn.serverline.lineicp.produce.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cn.serverline.lineicp.produce.entity.StepRequired;
import com.cn.serverline.lineicp.produce.entity.dto.PlanDetailsDTO;
import com.cn.serverline.lineicp.produce.entity.dto.PlanRequestDTO;
import com.cn.serverline.lineicp.produce.entity.vo.PlanDetailsVO;
import com.cn.serverline.lineicp.produce.entity.vo.PlanDetailsbVO;
import com.cn.serverline.lineicp.produce.entity.vo.StopDurationInfoVo;
import com.cn.serverline.lineicp.produce.entity.vo.TasksByLotNoVo;
import com.github.yulichang.base.MPJBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 工步所需 Mapper 接口
 * </p>
 *
 * @author 王烁
 * @since 2024-08-12
 */
@Mapper
public interface StepRequiredMapper extends MPJBaseMapper<StepRequired> {

    @Select("SELECT pwsdt.PROD_CONFIGURATION_NAME, pws.WORK_SECTION_NAME, pwsdt.LOT_NO, " +
            "COUNT(pwsdt.LOT_NO) AS lot_count, " +
            "MAX(plat.TASK_START_TIME) AS max_task_start_time, " +
            "MIN(plat.TASK_END_TIME) AS min_task_end_time, " +
            "MAX(plst.TASK_START_TIME) AS max_plst_task_start_time, " +
            "MIN(plst.TASK_END_TIME) AS min_plst_task_end_time " +
            "FROM PLAN_WEEK_STAGE_DAYS_TASKS pwsdt " +
            "LEFT JOIN PLAN_WORK_SECTION pws ON pwsdt.PLAN_WORK_SECTION_ID = pws.ID " +
            "LEFT JOIN PLAN_LINE_ASSEMBLE_BATCH_SPLIT plabs ON plabs.PLAN_WEEK_STAGE_DAYS_TASKS_ID = pwsdt.ID " +
            "LEFT JOIN PLAN_LINE_AUTOMATIC_TASKS plat ON plat.PLAN_LINE_ASSEMBLE_BATCH_SPLIT = plabs.ID " +
            "LEFT JOIN PLAN_LINE_ASSEMBLE_SPLIT plas ON plas.PLAN_WEEK_STAGE_DAYS_TASKS_ID = pwsdt.ID " +
            "LEFT JOIN PLAN_LINE_SCHEDULE_TASKS plst ON plas.ID = plst.PLAN_LINE_ASSEMBLE_SPLIT_ID " +
            "WHERE pwsdt.LOT_NO = #{lotNo} " +
            "GROUP BY pwsdt.PROD_CONFIGURATION_NAME, pws.WORK_SECTION_NAME, pwsdt.LOT_NO")
    List<TasksByLotNoVo> findByLotNo(String lotNo);

    @Select("SELECT\n" +
            "    su.REAL_NAME AS userName,\n" +
            "    ctpm.NAME AS ctpmName,\n" +
            "    ctpm.ID AS ctpmID,\n" + // 移除多余的注释符号
            "    tpse.END_TIME\n" + // 移除多余的逗号
            "FROM\n" +
            "    PLAN_WEEK_STAGE_DAYS_TASKS pwsdt\n" +
            "LEFT JOIN\n" +
            "    PLAN_LINE_ASSEMBLE_SPLIT plas ON plas.PLAN_WEEK_STAGE_DAYS_TASKS_ID = pwsdt.ID\n" +
            "LEFT JOIN\n" +
            "    PLAN_LINE_SCHEDULE_TASKS plst ON plas.ID = plst.PLAN_LINE_ASSEMBLE_SPLIT_ID\n" +
            "LEFT JOIN\n" +
            "    PLAN_LINE_SCHEDULE pls ON pls.PLAN_LINE_ASSEMBLE_SPLIT_ID = plas.ID\n" +
            "LEFT JOIN\n" +
            "    TE_PRODUCE_START_END tpse ON tpse.AUTOMATIC_SCHEDULE_ID = pls.ID\n" +
            "LEFT JOIN\n" +
            "    CT_PRODUCT_MIX ctpm ON ctpm.ID = plst.PROD_CONFIGURATION_ID\n" +
            "LEFT JOIN\n" +
            "    SYS_USER su ON su.USER_ID = pls.CREATE_USER\n" +
            "WHERE\n" +
            "    pwsdt.LOT_NO = #{lotNo}")
    List<StopDurationInfoVo> getAssembleDurationInfo(String lotNo);

    @Select("    SELECT\n" +
            "        su.REAL_NAME AS userName,\n" +
            "        'total' AS ctpmName,\n" +
            "        ctpm.ID AS ctpmID, -- 添加 ctpm.ID\n" +
            "        tpse.END_TIME\n" +
            "    FROM\n" +
            "        PLAN_WEEK_STAGE_DAYS_TASKS pwsdt\n" +
            "    LEFT JOIN\n" +
            "        PLAN_LINE_ASSEMBLE_BATCH_SPLIT plas ON plas.PLAN_WEEK_STAGE_DAYS_TASKS_ID = pwsdt.ID\n" +
            "    LEFT JOIN\n" +
            "        PLAN_LINE_AUTOMATIC_TASKS plst ON plas.ID = plst.PLAN_LINE_ASSEMBLE_BATCH_SPLIT\n" +
            "    LEFT JOIN\n" +
            "        PLAN_LINE_AUTOMATIC pls ON pls.PLAN_LINE_ASSEMBLE_BATCH_SPLIT = plas.ID\n" +
            "    LEFT JOIN\n" +
            "        TE_PRODUCE_START_END tpse ON tpse.AUTOMATIC_SCHEDULE_ID = pls.ID\n" +
            "    LEFT JOIN\n" +
            "        CT_PRODUCT_MIX ctpm ON ctpm.ID = plst.PROD_CONFIGURATION_ID\n" +
            "    LEFT JOIN\n" +
            "        SYS_USER su ON su.USER_ID = pls.CREATE_USER\n" +
            "    WHERE\n" +
            "        pwsdt.LOT_NO = #{lotNo}")
    List<StopDurationInfoVo> getStopDurationInfo(String lotNo);

    List<PlanDetailsVO> getPlanDetails(PlanRequestDTO requestDTO);

    List<PlanDetailsbVO>  getProductionTraceabilityDetails(PlanDetailsDTO requestDTO);
}
